import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'

import { useUserStore } from '@/stores'
import Layout from '@/views/layout/LayoutPage.vue'

const router = createRouter({
  
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [ 
    {path: '/login', component: ()=> import('@/views/login/LoginPage.vue')},
    { path: "/404",component: () => import("@/views/404.vue")},

    { path: '/' , component: Layout,
    redirect:'/dashboard',
    children: [
      {path: '/dashboard',
      component: () => import ('@/views/dashbench/index.vue')
    },
    {
      path: "/statistics",
      component: () => import("@/views/statistics/index.vue"),
    },
    {
      path: "/order",
      component: () => import("@/views/orderDetails/index.vue"),
    },
    {
      path: "/order/detail",
      component: () => import("@/views/orderDetails/details.vue"),
    },

    {
      path: "/setmeal",
      component: () => import("@/views/setmeal/index.vue"),
    },
    {
      path: "/setmeal/add",
          component: () => import("@/views/setmeal/addSetmeal.vue"),
    },
    {
      path:'/snacks',
      component: () => import ('@/views/snacks/index.vue')
    },
    {
      path:'/snacks/add',
      component: () => import ('@/views/snacks/addSnackstype.vue')
    },
    {
      path: "/category",
      component: () => import("@/views/category/index.vue"),
    },
    {
      path: "/employee",
      component: () => import("@/views/employee/index.vue"),
    },
    {
      path: "/employee/add",
      component: () => import("@/views/employee/addEmployee.vue"),
    }
    ]
  },
  ]
})

//登录访问拦截
router.beforeEach((to) =>{
  const userStore = useUserStore()
  if(!userStore.token && to.path !== '/login') return '/login'
})

export default router
